package com.fortune.controller;

import com.fortune.base.BaseController;
import com.fortune.common.paging.PagingData;
import com.fortune.dto.excution.WechatProductExcution;
import com.fortune.enums.WechatProductEnum;
import com.fortune.service.WechatProductSevice;
import com.fortune.util.UUIDUtil;
import com.fortune.vo.wechatProduct.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.apache.ibatis.annotations.Param;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "wechat/product", description = "商品相关", tags = {"微信小程序：商品管理"})
@RestController
@RequestMapping("wechat/product")
public class WechatProductController extends BaseController{
	@Autowired
	private WechatProductSevice wechatProductSevice;

	@ApiOperation(value = "商品列表", nickname = "wechatProductList", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true),
			@ApiImplicitParam(name="vendorId", value="厂商id",paramType="query",dataType="string"),
			@ApiImplicitParam(name="clientId", value="客户id",paramType="query",dataType="string")
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<?> list(@Validated String vendorId, @Validated @NotBlank(message="客户id不能为空") String clientId) {
		/*查询商品列表*/
		Map<String,List<Map<String,List<Product>>>> result = new HashMap<>();
		List<Map<String,List<Product>>> list = new ArrayList<>();
		result.put("List",list);
		Map<String,List<Product>> typeMap = new HashMap<>();
		list.add(typeMap);
		int j = 0;
		if(vendorId != null){
			List<ManufacturerProductList> findManufacturerProductList = wechatProductSevice.findManufacturerProductList(vendorId);
			for (ManufacturerProductList manufacturerProductList : findManufacturerProductList){

				List<Product> product = manufacturerProductList.getProducts();
				for(Product product1 : product){
					String did = getDId();
					String mpid = product1.getId();
					BigDecimal priceByCProduct = wechatProductSevice.priceByCProduct(did, clientId, vendorId, mpid);
					if(priceByCProduct == null){
						BigDecimal priceByDProduct = wechatProductSevice.priceByDProduct(did, vendorId, mpid);
						if(priceByDProduct == null){
							product1.setRetailPrice(BigDecimal.ZERO);
						}else{
							product1.setRetailPrice(priceByDProduct);
						}
					}else{
						product1.setRetailPrice(priceByCProduct);
					}


					TerminalDiscount findTerminalDiscount = wechatProductSevice.findTerminalDiscount(vendorId, mpid);
					if(findTerminalDiscount != null){
						product1.setPolicyId(findTerminalDiscount.getId());
						product1.setAchieveNum(findTerminalDiscount.getAchieveNum());
						product1.setGiftNum(findTerminalDiscount.getGiftNum());
					}
				}

				List<Product> products = new ArrayList<>();
				Product product2 = new Product();
				product2.setTypeName(manufacturerProductList.getName());
				products.add(product2);
				for (int i=0;i<product.size();i++){
					products.add(product.get(i));
				}
				typeMap.put("A" + String.valueOf(j++),products);
			}
		}else{
			List<ManufacturerProductList> findManufacturerProductList = wechatProductSevice.findZYProductList(getDId());
			for (ManufacturerProductList manufacturerProductList : findManufacturerProductList){

				List<Product> product = manufacturerProductList.getProducts();
				for(Product product1 : product){
					String did = getDId();
					String mpid = product1.getId();
					BigDecimal priceByCProduct = wechatProductSevice.priceByCProduct(did, clientId, did, mpid);
					if(priceByCProduct == null){
						BigDecimal priceByZYProduct = wechatProductSevice.priceByZYProduct(did, mpid);
						if(priceByZYProduct == null){
							product1.setRetailPrice(BigDecimal.ZERO);
						}else{
							product1.setRetailPrice(priceByZYProduct);
						}
					}else{
						product1.setRetailPrice(priceByCProduct);
					}


					TerminalDiscount findTerminalDiscount = wechatProductSevice.findTerminalDiscount(vendorId, mpid);
					if(findTerminalDiscount != null){
						product1.setPolicyId(findTerminalDiscount.getId());
						product1.setAchieveNum(findTerminalDiscount.getAchieveNum());
						product1.setGiftNum(findTerminalDiscount.getGiftNum());
					}
				}

				List<Product> products = new ArrayList<>();
				Product product2 = new Product();
				product2.setTypeName(manufacturerProductList.getName());
				products.add(product2);
				for (int i=0;i<product.size();i++){
					products.add(product.get(i));
				}
				typeMap.put("A" + String.valueOf(j++),products);
			}
		}

		return new WechatProductExcution<Map<String,List<Map<String,List<Product>>>>>(result, WechatProductEnum.FIND_SUCCESS);

	}

	@ApiOperation(value = "厂商列表", nickname = "wechatVendorList", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@GetMapping(value = "vendorList",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<VendorListVo>> vendorList() {
		String did = getDId();
		List<VendorListVo> findVendorListVoList = wechatProductSevice.findVendorListVoList(did);
		Integer findSelfSupport = wechatProductSevice.findSelfSupport(did);
		if (findSelfSupport != 0) {
			VendorListVo vendorListVo = new VendorListVo();
			vendorListVo.setId("ZY");
			vendorListVo.setName("自营");
			findVendorListVoList.add(vendorListVo);
			return new WechatProductExcution<List<VendorListVo>>(findVendorListVoList, WechatProductEnum.FIND_SUCCESS);
		}
		return new WechatProductExcution<List<VendorListVo>>(findVendorListVoList, WechatProductEnum.FIND_SUCCESS);
	}
}
